package com.lb.jwitter.action.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanh;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanhService;
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuLyLich;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuLyLichService;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinh;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinhService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAThongTinHocVienReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long tinhTrang;
    private Long khoaId;
    private Long nganhId;
    private Long chiNhanhId;
    
    private Long thongBaoId;
    
    private List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs;
    private TblQLDTTXAThongBaoTuyenSinhService tblQLDTTXAThongBaoTuyenSinhService;
    private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
    private TblQLDTTXADMNganhService tblQLDTTXADMNganhService;
    private TblQLDTTXADMChiNhanhService tblQLDTTXADMChiNhanhService;
    private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService;
    
    
    private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;
    private List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs;
    private List<TblQLDTTXADMChiNhanh> tblQLDTTXADMChiNhanhs;
    
    private String report;
    
    
    
    


  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_hocPhi", "xem")){ 
        return "noprivilege"; 
    } 
 
        tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
        tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
        tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
        tblQLDTTXAThongBaoTuyenSinhService = (TblQLDTTXAThongBaoTuyenSinhService) context.getBean("tbl_QLDTTXA_ThongBaoTuyenSinhClient"); 
        
//        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
//        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("ten");
            con.setValue1("asc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
        tblQLDTTXADMChiNhanhs = tblQLDTTXADMChiNhanhService.findtblQLDTTXADMChiNhanhByCondition(cons, 0, 0);
        tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findtblQLDTTXADMKhoaByCondition(cons, 0, 0);
        tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findtblQLDTTXADMNganhByCondition(cons, 0, 0);
        
        cons.clear();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhService.findtblQLDTTXAThongBaoTuyenSinhByCondition(cons, 30, 1);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXAThongTinHocVien Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
         return "noprivilege"; 
     } 
     tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context.getBean("tbl_QLDTTXA_HoSoXetTuyenClient"); 
         
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
                 
         //lay cac thong tin cua hoc vien
         cons.clear();
         
         if(thongBaoId != null && !thongBaoId.equals(new Long(0)) ){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("thongBaoId.id");
             con.setValue1(new Long(thongBaoId));
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(khoaId != null && !khoaId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("khoaId.id");
             con.setValue1(khoaId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(nganhId != null && !nganhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("nganhId.id");
             con.setValue1(nganhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(chiNhanhId != null && !chiNhanhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("chiNhanhId.id");
             con.setValue1(chiNhanhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         
         if(tinhTrang != null && !tinhTrang.equals(new Long(0))){
        	 if(tinhTrang.equals(new Long(1))){
        		 ConditionReport con = new ConditionReport(); 
                 con.setCol("trangThaiId");
                 con.setValue1(3);
                 con.setOperator("ne");
                 cons.add(con);	
        	 } else if(tinhTrang.equals(new Long(2))){
        		 ConditionReport con = new ConditionReport(); 
                 con.setCol("trangThaiId");
                 con.setValue1(3);
                 con.setOperator("eq");
                 cons.add(con);	
        	 }
         }
         
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         List<TblQLDTTXAHoSoXetTuyen> tblQLDTTXAHoSoXetTuyens = tblQLDTTXAHoSoXetTuyenService.findtblQLDTTXAHoSoXetTuyenByCondition(cons, 0, 0);
	     		  
		  report ="<tbody>";
		  int stt = 0;
		  DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		  
		  for(TblQLDTTXAHoSoXetTuyen hoSo : tblQLDTTXAHoSoXetTuyens){ 
			   stt++;
		      report += "<tr class='tableitem' > "; 
		      //stt
		      report += "<TD>" +stt+ "</TD>"; 
		      //mshv
		      report += "<td>" + (hoSo.getMaHoSo() == null ? "" : hoSo.getMaHoSo()) + "</td> ";
		      //ho ten
		      report += "<td>" + (hoSo.getHoTen() == null ? "" : hoSo.getHoTen())+ "</td> ";
		      report += "<td>" + (hoSo.getTen() == null ? "" : hoSo.getTen())+ "</td> ";
		      
		      report += "<td>" + (hoSo.getNgaySinh() == null ? "" : df.format(hoSo.getNgaySinh().toGregorianCalendar().getTime())) + "</td> ";
		      report += "<td>" + (hoSo.getNoiSinh() == null ? "" : hoSo.getNoiSinh()) + "</td> ";
		      report += "<td>" + (hoSo.isGioiTinh() ? "Nam" : "Nữ") + "</td> ";
		      report += "<td>" + (hoSo.getEmail() == null ? "" : hoSo.getEmail()) + "</td> ";
		      report += "<td>" + (hoSo.getDienThoai() == null ? "" : hoSo.getDienThoai()) + "</td> ";
		      report += "<td>" + (hoSo.getDiDong() == null ? "" : hoSo.getDiDong()) + "</td> ";
		      report += "<td>" + (hoSo.getDiaChi() == null ? "" : hoSo.getDiaChi()) + "</td> ";
		      report += "<td>" + (hoSo.getDiaChiLienLac() == null ? "" : hoSo.getDiaChiLienLac()) + "</td> ";
		      
		  }

		  report += "</tbody>"; 
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXAThongTinHocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}

public void setMsg(Message msg) {
	this.msg = msg;
} 
public Long getThongBaoId() {
	return thongBaoId;
}

public void setThongBaoId(Long thongBaoId) {
	this.thongBaoId = thongBaoId;
}

public List<TblQLDTTXAThongBaoTuyenSinh> getTblQLDTTXAThongBaoTuyenSinhs() {
	return tblQLDTTXAThongBaoTuyenSinhs;
}

public void setTblQLDTTXAThongBaoTuyenSinhs(
		List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs) {
	this.tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhs;
}

public Long getTinhTrang() {
	return tinhTrang;
}

public void setTinhTrang(Long tinhTrang) {
	this.tinhTrang = tinhTrang;
}
public Long getKhoaId() {
	return khoaId;
}

public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}

public Long getNganhId() {
	return nganhId;
}

public void setNganhId(Long nganhId) {
	this.nganhId = nganhId;
}

public Long getChiNhanhId() {
	return chiNhanhId;
}

public void setChiNhanhId(Long chiNhanhId) {
	this.chiNhanhId = chiNhanhId;
}

public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}

public List<TblQLDTTXADMNganh> getTblQLDTTXADMNganhs() {
	return tblQLDTTXADMNganhs;
}

public List<TblQLDTTXADMChiNhanh> getTblQLDTTXADMChiNhanhs() {
	return tblQLDTTXADMChiNhanhs;
} 
 } 
 